package cn.com.RabitMQ;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

/**
 * @author liufl
 * @version 创建时间：2017年7月17日 下午6:12:53
 * 广播
 */
public class SendFanout {
	private final static String HOST = MQConfig.serverUrl;  
    private final static String EXCHANGE_NAME = "fanout";  
    private final static String QUEUE = "temp_fanout";  
    private final static String ROUTKEY = "mq.fanout";  
    private final static boolean DURABLE = true;  
    public static void main(String[] args) throws IOException, TimeoutException{  
           // 创建连接和频道    
           ConnectionFactory factory = new ConnectionFactory();    
           factory.setHost(HOST);    
           Connection connection = factory.newConnection();    
           Channel channel = connection.createChannel();    
           // 声明转发器和类型    
           channel.exchangeDeclare(EXCHANGE_NAME, "fanout" );    
           //持久化  
           channel.queueDeclare(QUEUE, DURABLE, false, false, null);  
           channel.basicQos(1);  
           channel.queueBind(QUEUE, EXCHANGE_NAME, ROUTKEY);  
           String message = new Date().getTime()+" : fanout something";    
           // 往转发器上发送消息    
           channel.basicPublish(EXCHANGE_NAME, "", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());    
       
           System.out.println(" [x] Sent '" + message + "'");    
       
           channel.close();    
           connection.close();    
    }  
}

